package com.ly.heart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.StringUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.KingBaseCurrencyEntity;
import com.ly.heart.mapper.KingBankAccountMapper;
import com.ly.heart.service.IKingBankAccountService;
import com.ly.heart.service.IKingBaseCurrencyService;
import com.ly.heart.util.CurrentUserUtil;
import org.springframework.stereotype.Service;
import com.ly.heart.domain.KingBankAccountEntity;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 金蝶-财务模块-银行管理Service业务层处理
 * @author wutao
 * @date 2024-06-22
 */
@Service
public class KingBankAccountServiceImpl extends ServiceImpl<KingBankAccountMapper, KingBankAccountEntity> implements IKingBankAccountService {

    @Resource
    private IKingBaseCurrencyService iKingBaseCurrencyService;

    /**
     * 查询金蝶-财务模块-银行管理
     * @param id 金蝶-财务模块-银行管理ID
     * @return 金蝶-财务模块-银行管理
     */
    @Override
    public KingBankAccountEntity selectKingBankEntityById (String id) {
        KingBankAccountEntity bankAccountEntity = this.getById ( id );
        if ( StringUtils.isNotBlank ( bankAccountEntity.getCurrencyCode () ) ) {
            KingBaseCurrencyEntity one = iKingBaseCurrencyService.getOne ( new LambdaQueryWrapper<KingBaseCurrencyEntity> ().eq ( KingBaseCurrencyEntity :: getCode, bankAccountEntity.getCurrencyCode () ) );
            bankAccountEntity.setCurrencyName ( one.getCurrencyName () );
        }
        return bankAccountEntity;
    }

    /**
     * 查询金蝶-财务模块-银行管理列表
     * @param kingBankAccountEntity 金蝶-财务模块-银行管理
     * @return 金蝶-财务模块-银行管理
     */
    @Override
    public Page selectKingBankEntityList (KingBankAccountEntity kingBankAccountEntity) {
        LambdaQueryWrapper<KingBankAccountEntity> lambdaQueryWrapper = new LambdaQueryWrapper<> ();
        if ( StringUtils.isNotBlank ( kingBankAccountEntity.getBankCode () ) ) {
            lambdaQueryWrapper.eq ( KingBankAccountEntity::getBankCode, kingBankAccountEntity.getBankCode () );
        }
        lambdaQueryWrapper.eq ( KingBankAccountEntity::getIsUsed, ConstUtil.IS_USED_YES );
        Page<KingBankAccountEntity> page = this.page ( new Page<> ( kingBankAccountEntity.getPage (), kingBankAccountEntity.getLimit () ), lambdaQueryWrapper );
        return page;
    }

    /**
     * 新增金蝶-财务模块-银行管理
     * @param kingBankAccountEntity 金蝶-财务模块-银行管理
     * @return 结果
     */
    @Override
    public AjaxResult insertKingBankEntity (KingBankAccountEntity kingBankAccountEntity) {
        kingBankAccountEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        kingBankAccountEntity.setDeptId ( CurrentUserUtil.getCompanyId () );
        kingBankAccountEntity.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingBankAccountEntity.setCreateTime ( DateUtils.getNowDate () );
        boolean save = this.save ( kingBankAccountEntity );
        if ( ! save ) {
            throw new CustomException ( "新增银行账号失败！" );
        }
        return AjaxResult.success ();
    }

    /**
     * 修改金蝶-财务模块-银行管理
     * @param kingBankAccountEntity 金蝶-财务模块-银行管理
     * @return 结果
     */
    @Override
    public AjaxResult updateKingBankEntity (KingBankAccountEntity kingBankAccountEntity) {
        kingBankAccountEntity.setUpdateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingBankAccountEntity.setUpdateTime ( DateUtils.getNowDate () );
        boolean b = this.updateById ( kingBankAccountEntity );
        if ( ! b ) {
            throw new CustomException ( "修改银行账号失败！" );
        }
        return AjaxResult.success ();
    }

    /**
     * 批量删除金蝶-财务模块-银行管理
     * @param ids 需要删除的金蝶-财务模块-银行管理ID
     * @return 结果
     */
    @Override
    public AjaxResult deleteKingBankEntityByIds (String[] ids) {
        List<KingBankAccountEntity> list = this.list ( new LambdaQueryWrapper<KingBankAccountEntity> ().in ( KingBankAccountEntity :: getId, ids ) );
        //可删除ids集
        List<String> idList = list.stream ().filter ( account -> account.getIsDel () == 1 ).map ( KingBankAccountEntity :: getId ).collect ( Collectors.toList () );
        boolean update = this.update ( new LambdaUpdateWrapper<KingBankAccountEntity> ().
                set ( KingBankAccountEntity :: getIsUsed, ConstUtil.IS_USED_NO ).
                set ( KingBankAccountEntity :: getUpdateTime, DateUtils.getNowDate () ).
                set ( KingBankAccountEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                in ( KingBankAccountEntity :: getId, idList ) );
        if ( ! update ) {
            throw new CustomException ( "删除银行账号失败！" );
        }
        //不可删除名称
        StringBuilder stringBuilder = new StringBuilder ( "【" );
        list.forEach ( account -> {
            if ( account.getIsDel () == 2 ) {
                stringBuilder.append ( account.getTitle () + " " );
            }
        } );
        stringBuilder.append ( "】" );
        String str = "删除银行账户成功";
        if ( list.size () > 0 ) {
            str.concat ( "，其中"  ).concat ( stringBuilder.toString () ).concat ( "被引用，不可删除" );
        }
        return AjaxResult.success (str);
    }

    /**
     * 删除金蝶-财务模块-银行管理信息
     * @param id 金蝶-财务模块-银行管理ID
     * @return 结果
     */
    @Override
    public AjaxResult disabled (String id) {
        KingBankAccountEntity bankAccountEntity = this.getById ( id );
        if ( bankAccountEntity == null ) {
            throw new CustomException ( "无此银行账号！" );
        }
        boolean update = this.update ( new LambdaUpdateWrapper<KingBankAccountEntity> ().
                set ( KingBankAccountEntity :: getIsDisabled, bankAccountEntity.getIsDisabled () == 1 ? 2 : 1 ).
                set ( KingBankAccountEntity :: getUpdateTime, DateUtils.getNowDate () ).
                set ( KingBankAccountEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                eq ( KingBankAccountEntity :: getId, id ) );
        if ( ! update ) {
            throw new CustomException ( bankAccountEntity.getIsDisabled () == 1 ? "禁用银行账户失败！" : "解禁银行账号失败！" );
        }
        return AjaxResult.success ();
    }
}
